package com.sg.service.biz.component.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.component.req.BatchQueryStepReq;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import com.sg.entity.OmsAssistStep;
import com.sg.service.base.component.MOmsAssistStepService;
import com.sg.service.base.component.MOmsUnstructuredDataService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class OffStateServiceImpl
        implements com.wicket.okrcomponent.biz.service.OffStateService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MOmsAssistStepService mOmsAssistStepService;
    @Resource
    private MOmsUnstructuredDataService mOmsUnstructuredDataService;
    //@Resource
    //private MCustomFields3Service mCustomFields3Service;
    @Resource
    private NbOffState nbOffState;

    /**
     * D2-9查步骤详情(公共)(边界)[1821]
     * gen by moon at 10/26/2022, 12:18:53 AM
     */
    @Trace(operationName = "D2-9查步骤详情(公共)(边界)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryStepDetailComBorderRespDto queryStepDetailComBorder(QueryStepDetailComBorderReqDto reqDto) {


        OmsAssistStep omsAssistStep_1 = null;
//步骤0: 2-9-01查询步骤详情 - queryStepDetail
        OmsAssistStep omsAssistStep = null;
        QueryStepDetailReq queryStepDetailReq = new QueryStepDetailReq();
        if (reqDto != null) {
            queryStepDetailReq.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:55929_1
            queryStepDetailReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:55930_1
            queryStepDetailReq.setOrderNumber(reqDto.getOrderNumber());//SimpleFieldAssign//sourceId:55931_1
        }

        /*2-9-01查询步骤详情[864]   */

        omsAssistStep = mOmsAssistStepService.queryStepDetail(queryStepDetailReq);


        omsAssistStep_1 = omsAssistStep;

        QueryStepDetailComBorderRespDto retData = new QueryStepDetailComBorderRespDto();
        if (omsAssistStep_1 != null) {
            retData.setAssistStepId(omsAssistStep_1.getAssistStepId());//SimpleFieldAssign//sourceId:55955_1
            retData.setStepNameCode(omsAssistStep_1.getStepNameCode());//SimpleFieldAssign//sourceId:95639_1
        }


        return retData;
    }

    /**
     * D2-9查询步骤详情[871]
     * gen by moon at 2022/7/3 下午5:12:53
     */
    @Trace(operationName = "D2-9查询步骤详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryStepDetailRespDto queryStepDetail(QueryStepDetailReqDto reqDto) {


        OmsAssistStep omsAssistStep_1 = null;
//步骤0: 2-9-01查询步骤详情 - queryStepDetail
        OmsAssistStep omsAssistStep = null;
        QueryStepDetailReq queryStepDetailReq = new QueryStepDetailReq();
        if (reqDto != null) {
            queryStepDetailReq.setAssistStepId(reqDto.getAssistStepId());//sourceId:31528_1
        }

        /*2-9-01查询步骤详情[864]   */
        omsAssistStep = mOmsAssistStepService.queryStepDetail(queryStepDetailReq);

        omsAssistStep_1 = omsAssistStep;

        QueryStepDetailRespDto retData = new QueryStepDetailRespDto();
        if (omsAssistStep_1 != null) {
            retData.setAssistStepId(omsAssistStep_1.getAssistStepId());//sourceId:31542_1
            retData.setBizSceneId(omsAssistStep_1.getBizSceneId());//sourceId:31543_1
            retData.setBizSceneCode(omsAssistStep_1.getBizSceneCode());//sourceId:31544_1
            retData.setStepName(omsAssistStep_1.getStepName());//sourceId:31545_1
            retData.setStepNameCode(omsAssistStep_1.getStepNameCode());//sourceId:31546_1
            retData.setEditAssistStepName(omsAssistStep_1.getEditAssistStepName());//sourceId:31547_1
            retData.setChangeAssistStepName(omsAssistStep_1.getChangeAssistStepName());//sourceId:31548_1
            retData.setOrderNumber(omsAssistStep_1.getOrderNumber());//sourceId:31549_1
            retData.setSubjectLifeCycle(omsAssistStep_1.getSubjectLifeCycle());//sourceId:31550_1
            retData.setIsArchive(omsAssistStep_1.getIsArchive());//sourceId:31551_1
            retData.setOperationInductionId(omsAssistStep_1.getOperationInductionId());//sourceId:31552_1
            retData.setOperateTime(omsAssistStep_1.getOperateTime());//sourceId:31553_1
        }


        return retData;
    }

    /**
     * D2-9查步骤列表(公共)[872]
     * gen by moon at 10/10/2022, 3:34:50 AM
     */
    @Trace(operationName = "D2-9查步骤列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryStepListComRespDto queryStepListCom(QueryStepListComReqDto reqDto) {


        List<OmsAssistStep> listOmsAssistStep_1 = new ArrayList<>();
//步骤0: 2-9-01查询步骤列表 - queryStepList
        List<OmsAssistStep> listOmsAssistStep = new ArrayList<>();
        QueryStepListReq queryStepListReq = new QueryStepListReq();
        if (reqDto != null) {
            queryStepListReq.setBizSceneId(reqDto.getBizSceneId());//SimpleFieldAssign//sourceId:31455_1
            queryStepListReq.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:95290_1
            queryStepListReq.setStepName(reqDto.getStepName());//SimpleFieldAssign//sourceId:31456_1
            queryStepListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:31457_1
            queryStepListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:95294_1
        }

        /*2-9-01查询步骤列表[863]   */

        listOmsAssistStep = mOmsAssistStepService.queryStepList(queryStepListReq);


        listOmsAssistStep_1 = listOmsAssistStep;

        QueryStepListComRespDto retData = new QueryStepListComRespDto();
        retData.setStepList(listOmsAssistStep_1.stream().map(item -> BeanUtil.toBean(item, StepDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:31485_1


        return retData;
    }

    /**
     * D2-9新增步骤[870]
     * gen by moon at 2022/7/3 下午5:12:54
     */
    @Trace(operationName = "D2-9新增步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddStepRespDto addStep(AddStepReqDto reqDto) {


        String string_1 = null;
//步骤0: 2-9-01新增步骤 - addStep
        String string = null;
        OmsAssistStep omsAssistStep = new OmsAssistStep();
        if (reqDto != null) {
            omsAssistStep.setBizSceneId(reqDto.getBizSceneId());//sourceId:31060_1
            omsAssistStep.setBizSceneCode(reqDto.getBizSceneCode());//sourceId:31061_1
            omsAssistStep.setStepName(reqDto.getStepName());//sourceId:31063_1
            omsAssistStep.setStepNameCode(reqDto.getStepNameCode());//sourceId:31064_1
            omsAssistStep.setEditAssistStepName(reqDto.getEditAssistStepName());//sourceId:31065_1
            omsAssistStep.setChangeAssistStepName(reqDto.getChangeAssistStepName());//sourceId:31066_1
            omsAssistStep.setOrderNumber(reqDto.getOrderNumber());//sourceId:31067_1
            omsAssistStep.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:31068_1
        }

        /*2-9-01新增步骤[865]   */
        string = mOmsAssistStepService.addStep(omsAssistStep);

        string_1 = string;

        AddStepRespDto retData = new AddStepRespDto();
        if (string_1 != null) {
            retData.setAssistStepId(string_1);//sourceId:31069_1
        }


        return retData;
    }

    /**
     * D2-9修改步骤[869]
     * gen by moon at 2022/7/3 下午5:12:56
     */
    @Trace(operationName = "D2-9修改步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateStepRespDto updateStep(UpdateStepReqDto reqDto) {


        //步骤0: 2-9-01修改步骤 - updateStep
        boolean bOOLEAN;
        OmsAssistStep omsAssistStep = new OmsAssistStep();
        if (reqDto != null) {
            omsAssistStep.setAssistStepId(reqDto.getAssistStepId());//sourceId:31626_1
            omsAssistStep.setStepName(reqDto.getStepName());//sourceId:31627_1
            omsAssistStep.setStepNameCode(reqDto.getStepNameCode());//sourceId:31628_1
            omsAssistStep.setEditAssistStepName(reqDto.getEditAssistStepName());//sourceId:31629_1
            omsAssistStep.setChangeAssistStepName(reqDto.getChangeAssistStepName());//sourceId:31630_1
            omsAssistStep.setOrderNumber(reqDto.getOrderNumber());//sourceId:31631_1
            omsAssistStep.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:31632_1
        }

        /*2-9-01修改步骤[866]   */
        bOOLEAN = mOmsAssistStepService.updateStep(omsAssistStep);


        UpdateStepRespDto retData = new UpdateStepRespDto();


        return retData;
    }

    /**
     * D2-9查询高中基层步骤转数据集列表(公共)[4996]
     * gen by moon at 10/24/2022, 12:59:02 AM
     */
    @Trace(operationName = "D2-9查询高中基层步骤转数据集列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTopMidBasicStepShiftDataSetListComRespDto queryTopMidBasicStepShiftDataSetListCom(QueryTopMidBasicStepShiftDataSetListComReqDto reqDto) {


        List<OmsAssistStep> listOmsAssistStep_1 = new ArrayList<>();
//virtualUsage M2-获取接收字段  19917
        //ModelCode: receptionService
        ObtainReceiveFieldsRespDto receptionServiceRes = null;
        ObtainReceiveFieldsReqDto receptionServiceReq = new ObtainReceiveFieldsReqDto();
        if (reqDto != null) {
            receptionServiceReq.setBatchOperateScene(reqDto.getBatchOperateScene());//SimpleFieldAssign//sourceId:388465_1_19917
        }

        /*M2-获取接收字段[4007]  用于特殊方法接收上游入参。 */
        Assert.isNull(receptionServiceReq.getBatchOperateScene(), "D2-9查询高中基层步骤转数据集列表(公共)-M2-获取接收字段-高/中/基层操作场景不能为空", false);
        receptionServiceRes = nbOffState.obtainReceiveFields(receptionServiceReq);


        if ((reqDto != null && reqDto.getBatchOperateScene() != null && reqDto.getBatchOperateScene().equals("TOP"))) {
            //if(D2-9查询高中基层步骤转数据集列表(公共).高/中/基层操作场景 等于 高层)  19918

//ModelCode: rowToColumn
            TransitionStepRespDto rowToColumnRes = null;
            TransitionStepReqDto rowToColumnReq = new TransitionStepReqDto();
            rowToColumnReq.setStepCode1("COMPILE_TARGET_TASK");//CUSTOM_CONVENTION//sourceId:388439_1_19919
            rowToColumnReq.setStepCode2("TARGET_TASK_DIV");//CUSTOM_CONVENTION//sourceId:388440_1_19919
            rowToColumnReq.setStepCode3("DEPT_TARGET_WEIGHT_SET");//CUSTOM_CONVENTION//sourceId:388441_1_19919
            rowToColumnReq.setStepCode4("PALN_COMPILE_ACHIEVE");//CUSTOM_CONVENTION//sourceId:388442_1_19919

            /*2-9评价模板高层步骤标识行转列[4997]  导入变更字段使用，将excel每行数据转换成列 */
            Assert.isNull(rowToColumnReq.getStepCode1(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9评价模板高层步骤标识行转列-步骤标识1不能为空", false);
            Assert.isNull(rowToColumnReq.getStepCode2(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9评价模板高层步骤标识行转列-步骤标识2不能为空", false);
            Assert.isNull(rowToColumnReq.getStepCode3(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9评价模板高层步骤标识行转列-步骤标识3不能为空", false);
            Assert.isNull(rowToColumnReq.getStepCode4(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9评价模板高层步骤标识行转列-步骤标识4不能为空", false);
            rowToColumnRes = nbOffState.transitionStep(rowToColumnReq);


            List<OmsAssistStep> listOmsAssistStep = new ArrayList<>();
            BatchQueryStepReq batchQueryStepReq = new BatchQueryStepReq();
            batchQueryStepReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:388472_1_19920
            if (rowToColumnRes != null) {
                batchQueryStepReq.setStepList(rowToColumnRes.getStepList());//list-field-assign//sourceId:388471_1_19920
            }

            /*2-9批量查询步骤[4998]   */
            Assert.isNull(batchQueryStepReq.getSubjectLifeCycle(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9批量查询步骤-主体生命周期不能为空", false);
            listOmsAssistStep = mOmsAssistStepService.batchQueryStep(batchQueryStepReq);


            listOmsAssistStep_1 = listOmsAssistStep;
        } else if ((reqDto != null && reqDto.getBatchOperateScene() != null && reqDto.getBatchOperateScene().equals("MID"))) {
            //elseif(D2-9查询高中基层步骤转数据集列表(公共).高/中/基层操作场景 等于 中层)  19921

//ModelCode: rowToColumn
            TransitionStepRespDto rowToColumnRes_2 = null;
            TransitionStepReqDto rowToColumnReq_1 = new TransitionStepReqDto();
            rowToColumnReq_1.setStepCode1("LEAD_TARGET_ARRANGE");//CUSTOM_CONVENTION//sourceId:388474_1_19922
            rowToColumnReq_1.setStepCode2("EXECUTE_TARGET_ARRANGE");//CUSTOM_CONVENTION//sourceId:388475_1_19922
            rowToColumnReq_1.setStepCode3("TARGET_TASK_DIV");//CUSTOM_CONVENTION//sourceId:388476_1_19922
            rowToColumnReq_1.setStepCode4("ACHIEVE_ARRANGE");//CUSTOM_CONVENTION//sourceId:388477_1_19922

            /*2-9中层步骤标识转数据集[4997]  导入变更字段使用，将excel每行数据转换成列 */
            Assert.isNull(rowToColumnReq_1.getStepCode1(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9中层步骤标识转数据集-步骤标识1不能为空", false);
            Assert.isNull(rowToColumnReq_1.getStepCode2(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9中层步骤标识转数据集-步骤标识2不能为空", false);
            Assert.isNull(rowToColumnReq_1.getStepCode3(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9中层步骤标识转数据集-步骤标识3不能为空", false);
            Assert.isNull(rowToColumnReq_1.getStepCode4(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9中层步骤标识转数据集-步骤标识4不能为空", false);
            rowToColumnRes_2 = nbOffState.transitionStep(rowToColumnReq_1);


            List<OmsAssistStep> listOmsAssistStep_2 = new ArrayList<>();
            BatchQueryStepReq batchQueryStepReq_1 = new BatchQueryStepReq();
            batchQueryStepReq_1.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:388472_1_19923
            if (rowToColumnRes_2 != null) {
                batchQueryStepReq_1.setStepList(rowToColumnRes_2.getStepList());//list-field-assign//sourceId:388471_1_19923
            }

            /*2-9批量查询步骤[4998]   */
            Assert.isNull(batchQueryStepReq_1.getSubjectLifeCycle(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9批量查询步骤-主体生命周期不能为空", false);
            listOmsAssistStep_2 = mOmsAssistStepService.batchQueryStep(batchQueryStepReq_1);


            listOmsAssistStep_1 = listOmsAssistStep_2;
        } else if ((reqDto != null && reqDto.getBatchOperateScene() != null && reqDto.getBatchOperateScene().equals("BASIC"))) {
            //elseif(D2-9查询高中基层步骤转数据集列表(公共).高/中/基层操作场景 等于 基层)  19924

//ModelCode: rowToColumn
            TransitionStepRespDto rowToColumnRes_4 = null;
            TransitionStepReqDto rowToColumnReq_2 = new TransitionStepReqDto();
            rowToColumnReq_2.setStepCode1("EXECUTE_TARGET_ARRANGE");//CUSTOM_CONVENTION//sourceId:388482_1_19925
            rowToColumnReq_2.setStepCode2("TARGET_TASK_DIV");//CUSTOM_CONVENTION//sourceId:388483_1_19925
            rowToColumnReq_2.setStepCode3("ACHIEVE_ARRANGE");//CUSTOM_CONVENTION//sourceId:388484_1_19925

            /*2-9基层步骤标识转数据集[4997]  导入变更字段使用，将excel每行数据转换成列 */
            Assert.isNull(rowToColumnReq_2.getStepCode1(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9基层步骤标识转数据集-步骤标识1不能为空", false);
            Assert.isNull(rowToColumnReq_2.getStepCode2(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9基层步骤标识转数据集-步骤标识2不能为空", false);
            Assert.isNull(rowToColumnReq_2.getStepCode3(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9基层步骤标识转数据集-步骤标识3不能为空", false);
            rowToColumnRes_4 = nbOffState.transitionStep(rowToColumnReq_2);


            List<OmsAssistStep> listOmsAssistStep_3 = new ArrayList<>();
            BatchQueryStepReq batchQueryStepReq_2 = new BatchQueryStepReq();
            batchQueryStepReq_2.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:388472_1_19926
            if (rowToColumnRes_4 != null) {
                batchQueryStepReq_2.setStepList(rowToColumnRes_4.getStepList());//list-field-assign//sourceId:388471_1_19926
            }

            /*2-9批量查询步骤[4998]   */
            Assert.isNull(batchQueryStepReq_2.getSubjectLifeCycle(), "D2-9查询高中基层步骤转数据集列表(公共)-2-9批量查询步骤-主体生命周期不能为空", false);
            listOmsAssistStep_3 = mOmsAssistStepService.batchQueryStep(batchQueryStepReq_2);


            listOmsAssistStep_1 = listOmsAssistStep_3;
        }
//processBranchName:正常结束 ,processBranchId:19928

        QueryTopMidBasicStepShiftDataSetListComRespDto retData = new QueryTopMidBasicStepShiftDataSetListComRespDto();
        retData.setStepList(listOmsAssistStep_1.stream().map(item -> BeanUtil.toBean(item, StepDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:388490_1


        return retData;
    }

    /**
     * D2-9查询评价主题步骤列表[5000]
     * gen by moon at 10/24/2022, 4:34:46 AM
     */
    @Trace(operationName = "D2-9查询评价主题步骤列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryEvaThemeStepListRespDto queryEvaThemeStepList(QueryEvaThemeStepListReqDto reqDto) {


        List<OmsAssistStep> listOmsAssistStep_1 = new ArrayList<>();
//步骤0: 2-9-01查询步骤列表 - queryStepList
        List<OmsAssistStep> listOmsAssistStep = new ArrayList<>();
        QueryStepListReq queryStepListReq = new QueryStepListReq();
        queryStepListReq.setBizSceneCode("ADD_EVA_TEMP");//CUSTOM_CONVENTION//sourceId:388517_1
        queryStepListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:388516_1

        /*2-9-01查询步骤列表[863]   */
        Assert.isNull(queryStepListReq.getBizSceneCode(), "D2-9查询评价主题步骤列表-2-9-01查询步骤列表-业务场景标识不能为空", false);
        Assert.isNull(queryStepListReq.getSubjectLifeCycle(), "D2-9查询评价主题步骤列表-2-9-01查询步骤列表-主体生命周期不能为空", false);
        listOmsAssistStep = mOmsAssistStepService.queryStepList(queryStepListReq);


        listOmsAssistStep_1 = listOmsAssistStep;

        QueryEvaThemeStepListRespDto retData = new QueryEvaThemeStepListRespDto();
        retData.setStepList(listOmsAssistStep_1.stream().map(item -> BeanUtil.toBean(item, StepDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:388518_1


        return retData;
    }
    //
}
